/**
 * Not technically a "preloader".  But is an intermediate stage
 * where we can load the friend information & anything else before drawing the room.
 */
package
{
	import flash.events.Event;
	import flash.filters.GlowFilter;
	
	import org.flixel.*;

	public class FacebookPreloader extends FlxState
	{

		private var registerComplete:Boolean;
		private var friendsComplete:Boolean;
		private var outOfTutorial:Boolean;
		private var tutorialLevel:int;
		private var titleScreen:FlxSprite;
		private var playButton:FlxSprite;
		private var playClicked:Boolean;
		protected var hoverFilter:GlowFilter;
		protected var playOverlapped:Boolean;
		protected var fromTut:Boolean;
		
		public function FacebookPreloader(fromTut:Boolean = false)
		{
			registerComplete = false;
			friendsComplete = false;
			outOfTutorial = false;
			playClicked = false;
			this.fromTut = fromTut;
			
			add(new FlxSprite(0, 0, Res.LoadingScreen));
			var li:LoadingIndicator = new LoadingIndicator(0, 0, LoadingIndicator.ORANGE);
			li.x = (FlxG.width - li.width) / 2;
			li.y = 480;
			add(li);
			
			if (!fromTut) {
				titleScreen = new FlxSprite(0,0,Res.facebookTitle);
				add(titleScreen);
			
				playButton = new FlxSprite(225,375,Res.superPlayButton);
				this.add(playButton);
			} else {
				playClicked = true;
			}
			
			hoverFilter = new GlowFilter(0x00FFF6, 0.6, 80, 80, 3,1);

			
			if (!Elemental.DEBUG) {
				Elemental.fbInfo.userName = FlxG.stage.root.loaderInfo.parameters.usr_name;
				Elemental.fbInfo.userID = FlxG.stage.root.loaderInfo.parameters.facebook_id;
				Elemental.fbInfo.fullName = FlxG.stage.root.loaderInfo.parameters.name;
				Elemental.fbInfo.token = FlxG.stage.root.loaderInfo.parameters.accessToken;
				Elemental.Log.changeUID();
			}

			FlxG.mouse.show();
			
			// Register this user 
			Client.registerUser(Elemental.fbInfo.userID, function (e:Event):void {
				trace("register complete");
				//for achievements
				var stats:String = e.target.data;
				trace(stats);
				if (stats.charAt(0) != "E") {
					var statArray:Array = stats.split(":");
					Elemental.achievements.initializeWithStats(
						statArray[0], //level
						statArray[1], //monsters
						statArray[2], //chickens
						statArray[3], //challenges beaten
						statArray[4], //challenges issued
						statArray[5], //pacifist
						statArray[6], //maps created
						statArray[7], //maps beated
						statArray[8], //friends beaten
						statArray[9]); //invites
					
					tutorialLevel = int(statArray[10]);
					if (tutorialLevel > 4) {//tutorial
						outOfTutorial = true;
					}
				}
				registerComplete = true;
				checkLoadingComplete();
			});
			// Get friends
			Elemental.fbInfo.getFriends();
			Elemental.fbInfo.addEventListener(FaceBook.FRIENDS_LOADED, function f():void {
				Elemental.fbInfo.removeEventListener(FaceBook.FRIENDS_LOADED, f);
				trace("get friends complete");
				friendsComplete = true;
				checkLoadingComplete();
			});
		}
		
		public override function update():void {
			super.update();
			
			if (FlxG.mouse.justPressed() && !fromTut && playButton.overlapsPoint(FlxG.mouse.x, FlxG.mouse.y)) {
				this.remove(titleScreen, true);
				this.remove(playButton, true);
				playClicked = true;
				checkLoadingComplete();
			}
			if (!playOverlapped && !fromTut && playButton.overlapsPoint(FlxG.mouse.x, FlxG.mouse.y)) {
				playButton.setFilter(hoverFilter);
				playOverlapped = true;
			}
			if (playOverlapped && !fromTut && !playButton.overlapsPoint(FlxG.mouse.x, FlxG.mouse.y)) {
				playButton.removeFilter();
				playOverlapped = false;
			}
		}
		
		public function checkLoadingComplete():void {
			trace("r " + registerComplete + " f " + friendsComplete + " p " + playClicked + " o " + outOfTutorial);
			if (registerComplete && friendsComplete && playClicked && outOfTutorial) {
				FlxG.switchState(new PlayerRoomState());
			} else if (playClicked && registerComplete && !outOfTutorial) {
				FlxG.switchState(new TutorialPlayingState(Elemental.fbInfo.userID, tutorialLevel));
			}
		}
	}
}